package com.uh.microservice.core.spring.di;


import akka.actor.ActorSystem;
import com.uh.microservice.core.spring.SpringUtil;
import com.uh.microservice.core.util.LogUtil;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

/**
 * akka的全局配置类
 * Created by liming on 2017/9/15.
 */
@Configuration
public class ApplicationConfiguration {
    private LogUtil log=new LogUtil(ApplicationConfiguration.class);
    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private SpringExtension springExtension;

    /**
     *  akka外部配置文件的读取及配置信息的返回
     */
    @Bean
    public Config akkaConfiguration(){
        Config conf=ConfigFactory.load();
        try {
            String path = SpringUtil.getProp("spring.application.rule_path") + "/config/akka.conf";
            Config otherConf = ConfigFactory.parseFile(new File(path));
            if (otherConf.entrySet().size() > 0) {
                log.info("外部AKKA配置文件加载成功");
                return otherConf;
            }else
                log.info("外部AKKA配置文件加载为空");
        }catch (Exception e){
            log.info("外部AKKA配置文件加载失败");
        }
        return conf;
    }

    /**
     *  akka创建actor的工厂方法
     */
    @Bean
    public ActorSystem actorSystem() {
        ActorSystem actorSystem = ActorSystem.create("ct3-actor-system", akkaConfiguration());
        log.debug( actorSystem.settings().toString() );
        springExtension.initialize(applicationContext);
        return actorSystem;
    }
}
